package com.kinon.bs_app.activities;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kinon.bs_app.R;
import com.kinon.bs_app.beans.CodeBean;
import com.kinon.bs_app.utils.BaseActivity;
import com.kinon.bs_app.utils.HttpUtils;
import com.kinon.bs_app.utils.PrefUtils;
import com.kinon.bs_app.utils.http.StrCallback;

import retrofit2.Call;
import retrofit2.Response;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    private Button btn_back = null;

    private EditText et_phone = null;
    private TextView tv_getCode = null;

    private EditText et_code = null;
    private EditText et_password = null;
    private EditText et_ivCode = null;

    private Button btn_finish = null;

    private static String CODE = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initListener();
    }

    private void initView() {
        btn_back = findViewById(R.id.btn_back);

        et_phone = findViewById(R.id.et_phone);
        tv_getCode = findViewById(R.id.tv_getCode);

        et_code = findViewById(R.id.et_code);
        et_password = findViewById(R.id.et_password);
        et_ivCode = findViewById(R.id.et_ivCode);

        btn_finish = findViewById(R.id.btn_finish);
    }

    private void initListener() {
        btn_back.setOnClickListener(this);
        tv_getCode.setOnClickListener(this);
        btn_finish.setOnClickListener(this);
    }

    private void getCode() {
        String phone = et_phone.getText().toString().trim();
        if (phone.isEmpty()) {
            showToast("手机号不能为空");
            return;
        }

        Call<String> call = HttpUtils.getRequestApi().sendCode_APP(phone);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                Log.d("zzy", "data: "+data);
                Gson gson = new Gson();
                CodeBean bean = gson.fromJson(data, CodeBean.class);
                CODE = bean.getCode();
            }

            @Override
            public void onError(int code, String error) {
                Log.d("zzy", "error: "+error);
                showToast(error);
            }
        });
    }

    private void submit() {
        final String phone = et_phone.getText().toString().trim();
        String code = et_code.getText().toString().trim();
        final String password = et_password.getText().toString().trim();
        String ivCode = et_ivCode.getText().toString().trim();
        if (phone.isEmpty()) {
            showToast("手机号不能为空");
            return;
        }
        if (code.isEmpty()) {
            showToast("验证码不能为空");
            return;
        }
        if (password.isEmpty()) {
            showToast("密码不能为空");
            return;
        }
        if (!code.equals(CODE)) {
            showToast("验证码错误");
            return;
        }

        Call<String> call = HttpUtils.getRequestApi().save_APP(phone, password, ivCode);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                Log.d("zzy", "tip: "+"<注册成功> "+data);
                showToast("用户注册成功");
                PrefUtils.putString("phone", phone);
                PrefUtils.putString("password", password);
//                PrefUtils.putString("token", data);
                finish();
            }

            @Override
            public void onError(int code, String error) {
                Log.d("zzy", "error: "+"<注册失败> "+error);
                showToast(error);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.tv_getCode:
                getCode();
                break;
            case R.id.btn_finish:
                submit();
                break;
            default:
                break;
        }
    }
}
